<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">主页</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>
    <el-card class="box-card" style="margin-top:20px">
      <el-row type="flex">
        <el-col :span="12">
          <span>用户名:</span>
          <el-input v-model="search.name" placeholder="请输入用户名" />
        </el-col>
        <el-col :span="12">
          <span>联系方式:</span>
          <el-select v-model="search.telephone" placeholder="请选择">
            <el-option
              v-for="(telephone,index) in options"
              :key="index"
              :label="telephone"
              :value="telephone"
            />
          </el-select>
        </el-col>
      </el-row>
      <el-row type="flex" justify="end" style="margin-top:20px">
        <el-button type="primary" size="small" @click="btnSearch">查询</el-button>
        <el-button size="small" @click="btnReset">重置</el-button>
      </el-row>
      <el-button type="primary" size="small" style="margin-bottom:20px" @click="btnAdd">+ 新增</el-button>
      <el-tabs v-model="activeName">
        <el-tab-pane label="账户设置" name="first">
          <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="70"
            />
            <el-table-column
              prop="username"
              label="用户名"
              width="200"
            />
            <el-table-column
              prop="mobile"
              label="电话号码"
              width="200"
            />
            <el-table-column
              prop="email"
              label="邮箱"
              show-overflow-tooltip
            />
            <el-table-column label="操作">
              <template #default="{row}">
                <el-button
                  size="mini"
                  type="warning"
                  @click="handleEdit(row.id)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="center">
            <el-pagination
              style="margin-top:20px"
              :current-page="page.page"
              :page-sizes="[10, 15, 20, 30]"
              :page-size="page.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="参数设置" name="second">配置管理</el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog
      :title="`${show}`"
      :visible.sync="showDiglog"
      width="50%"
      @close="btnCancle"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="ruleForm.mobile" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDiglog = false">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { getUserDetail, delUser, getUserInfo, getTheUser, editUser, addUser } from '@/api/settings'
export default {
  data() {
    return {
      activeName: 'first',
      showDiglog: false,
      tableData: [],
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      ruleForm: {
        username: '',
        password: '',
        mobile: '',
        email: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '用户名字符长度为2个到6个', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        mobile: [{ required: true, message: '联系方式不能为空', trigger: 'blur' }]
      },
      options: [],
      search: {
        name: '',
        telephone: ''
      }
    }
  },
  computed: {
    show() {
      return this.ruleForm.id ? '编辑用户' : '添加用户'
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 获取用户列表
    getUserList() {
      getUserDetail().then((res) => {
        this.page.total = res.total
        var newArr = []
        res.data.forEach(value => {
          if (newArr.indexOf(value.mobile) === -1) {
            newArr.push(value.mobile)
          }
        })
        this.options = newArr
        this.tableData = res.data
      })
    },
    // 点击添加按钮
    btnAdd() {
      this.showDiglog = true
    },
    // 添加用户
    btnOk() {
      this.$refs.ruleForm.validate().then(() => {
        if (this.ruleForm.id) {
          editUser(this.ruleForm).then(() => {
            this.$message.success('修改用户信息成功')
            this.getUserList()
            this.btnCancle()
          }).catch((err) => {
            this.$message.error(err.message)
          })
        } else {
          const arr = this.tableData.filter(value => {
            this.ruleForm.username === value.username || this.ruleForm.mobile === value.mobile
          })
          if (arr) {
            return this.$message.error('用户名或电话号码已被占用')
          }
          addUser(this.ruleForm).then((res) => {
            this.$message.success('添加用户成功')
            this.getUserList()
            this.btnCancle()
          }).catch((err) => {
            this.$message.error(err.message)
          })
        }
      }).catch(() => {
        this.$message.error('验证失败')
      })
    },
    // 点击搜索
    btnSearch() {
      if (this.search.name || this.search.telephone) {
        getUserInfo({ username: this.search.name, mobile: this.search.telephone }).then((res) => {
          this.tableData = res.data
        })
      }
    },
    // 点击重置
    btnReset() {
      if (this.search.name || this.search.telephone) {
        this.search.name = ''
        this.search.telephone = ''
        this.getUserList()
      }
    },
    // 关闭弹层
    btnCancle() {
      this.ruleForm = {
        username: '',
        password: '',
        mobile: '',
        email: ''
      }
      this.showDiglog = false
    },
    handleSelectionChange() {

    },
    // 点击编辑获取当前用户信息
    handleEdit(id) {
      getTheUser(id).then((res) => {
        this.ruleForm = res.data
        this.showDiglog = true
      }).catch((err) => {
        this.$message.error(err.message)
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除吗？').then(() => {
        delUser(id).then(() => {
          this.$message.success('删除成功')
          this.getUserList()
        })
      }).catch(() => {
        this.$message.error('取消删除')
      })
    },
    handleSizeChange(size) {
      this.page.size = size
      getUserDetail(this.page).then((res) => {
        this.tableData = res.data
      })
    },
    handleCurrentChange(current) {
      this.page.page = current
      getUserDetail(this.page).then((res) => {
        this.tableData = res.data
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
    min-width: 1150px;
   .el-row {
    margin: 0 100px;
    .el-col-12 {
        display: flex;
        align-items: center;
        justify-content: space-around;
        span {
            flex: 1;
        }
        .el-input {
            flex: 2;
            margin-right: 250px;
        }
        .el-select {
            flex: 2;
            margin-right: 220px;
        }
    }
  }
}

</style>
